/**
 *
 */
package com.jk.vehicle.core.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ShenHuaJie
 * @version 2017年12月12日 下午4:42:52
 */
public class FileUtil {

    /**
     * 文档类型
     */
    public static final String DOC = "docx";

    /**
     * pdf文件
     */
    public static final String PDF = "pdf";

    /**
     * 日志
     */
    private static Logger logger = LogManager.getLogger();

    /**
     * 根据文件名读取文件
     * @param fileName
     * @return
     */
    public static List<String> readFile(String fileName) {
        List<String> list = new ArrayList<String>();
        BufferedReader reader = null;
        FileInputStream fis = null;
        try {
            File f = new File(fileName);
            if (f.isFile() && f.exists()) {
                fis = new FileInputStream(f);
                reader = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (!"".equals(line)) {
                        list.add(line);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("readFile", e);
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                logger.error("InputStream关闭异常", e);
            }
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                logger.error("FileInputStream关闭异常", e);
            }
        }
        return list;
    }

    /***
     * 文件相对资源文件路径
     */
    public static final String FILE_PATH = "attachment";

    /**
     * 时间格式
     */
    private static String timeFormat = "yyyy-MM-ddHH_mm_ss";

    /**
     * 保存word和 pdf 文件文件名
     * @param file
     * @param request
     * @return
     */
    public static String saveFile(MultipartFile file, HttpServletRequest request) throws IOException {
        String projectPath = System.getProperty("user.dir");
        File path = new File(projectPath);
        File resourcesPath = new File(URLDecoder.decode(path.getPath(), "UTF-8") + File.separator + FILE_PATH);
        if (!resourcesPath.exists()) {
            resourcesPath.mkdirs();
        }
        String time = TimeUtil.getCurrentTime(timeFormat);
        File imageFile = new File(resourcesPath, time + file.getOriginalFilename());
        if (!imageFile.exists()) {
            imageFile.createNewFile();
        }
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(imageFile));
        out.write(file.getBytes());
        out.flush();
        out.close();
        String fileName = time + file.getOriginalFilename();
        return fileName;
    }

    /**
     * 获取文件后缀名
     * @param file
     * @return
     */
    public static String getFileType(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        int position = fileName.lastIndexOf(".") + 1;
        return fileName.substring(position).toLowerCase();
    }

    /**
     * 获取文件后缀名
     * @param name 文件名
     * @return 文件后缀
     */
    public static String getFileType(String name) {
        int positon = name.lastIndexOf(".") + 1;
        return name.substring(positon).toLowerCase();
    }
}
